1. Understanding cmdlets
Window PowerShell
cmdlets are utilities that are built on top of application interfaces,
and in the case of SharePoint 2010, the SharePoint Object Module, and
SharePoint Web Services, that allow administrators to complete a number
of tasks.
In general, cmdlets use a
verb-noun pair. The noun specifies the object you want information
about or that you want to manipulate, and the verb states what you want
to do with that object. The verbs and nouns are always separated by a
hyphen with no spaces, and SharePoint cmdlet verbs have a prefix of SP.
For example, if you want to get (the verb) information about all the
content databases (noun) in your farm, you would use the following
SharePoint cmdlet, which would give you the output shown.
PS C:\Users\Peter> Get-SPContentDatabase
Id : a1d5c96c-a41a-43b3-bc5d-3f8a93b26046
Name : WSS_Content_a2fde53006e04bf5aae434ffd3c8a19c
WebApplication : SPWebApplication Name=SharePoint - 80
Server : SQL
CurrentSiteCount : 1
Id : 363b11a3-6947-42f6-9df4-665eeff59c83
Name : SPF_TeamsDB
WebApplication : SPWebApplication Name=SPF_Teams
Server : SQL
CurrentSiteCount : 1
Windows PowerShell also provides a few cmdlets that help you to work with the other cmdlets. Two examples of these are Get-Command, which can be shorted to gcm, and Get-Help, which can be shorted to help. (Shortened names of cmdlets are called aliases.)
Get-Command allows you to find cmdlets, and then Get-Help can provide
you with basic information about a cmdlet after it is retrieved. In the following example, the Get-Command finds all the cmdlets associated with SharePoint.
PS C:\Users\Peter>Get-Command -PSSnapin "Microsoft.SharePoint.PowerShell" |
>>sort noun, verb |Format-Wide -Column 3
>><ENTER>
Note:
When you type a
one-line Windows PowerShell script, or you have not provided all the
required parameters, Windows PowerShell either prompts you for the
parameter or starts a new line with >> so that you can enter more
commands. When you have finished typing, press Enter for Windows
PowerShell to execute the code. Because of page size restrictions, many
of the commands in this article are shown on multiple lines when they
could be typed on one line, so always remember to press Enter only to
execute the scripts.
The set of commands shown previously takes the output from the Get-Command cmdlet and pipes it to the sort cmdlet.
The sort cmdlet sorts the data in noun-then-verb order so that all
SharePoint cmdlets that manipulate the same object are listed together.
The cmdlets that manipulate the same object are then sorted in verb
order. To reduce the amount of scrolling necessary in the SharePoint
2010 Management Shell, the sorted data is piped to the format-wide
cmdlet. Such a combination of Windows PowerShell cmdlets is quite
common. The output from the command set would look similar to the
following sample.
Start-SPAdminJob Get-SPAlternateURL New-SPAlternateURL
Remove-SPAlternateURL Set-SPAlternateURL Install-SPApplicationC...
Start-SPAssignment Stop-SPAssignment Get-SPAuthenticationPr...
New-SPAuthenticationPro... Get-SPBackupHistory Move-SPBlobStorageLoca...
Get-SPBrowserCustomerEx... Set-SPBrowserCustomerEx... Copy-SPBusinessDataCat...
.....(not all output shown)
Set-SPWebApplicationHtt... Get-SPWebPartPack Install-SPWebPartPack
Uninstall-SPWebPartPack Get-SPWebTemplate Install-SPWebTemplate
Set-SPWebTemplate Uninstall-SPWebTemplate Get-SPWorkflowConfig
Set-SPWorkflowConfig
Note:
There is another cmdlet
similar to the Get-Command that helps you work with cmdlets and that
you might find useful—especially if you create your own commands or
scripts. It is the Measure-Command cmdlet. This command allows you to measure the time it takes to run cmdlets or scripts. You may also find the Trace-Command cmdlet useful for debugging short script blocks.